package com.hll.javaee.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class FormServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    // 获取参数
    String name = request.getParameter("username");
    String pwd = request.getParameter("pwd");
    String[] hobbies = request.getParameterValues("hobbies");
    System.out.println(name + "," + pwd);
    System.out.println(Arrays.toString(hobbies));

    request
        .getRequestDispatcher("/success.jsp")
        .forward(request, response); // 转发给jsp也行, 这里不能写 /s2/.jsp, 只能/jsp 或 jsp
    // response.sendRedirect("success.jsp"); // 重定向至success.jsp
    // 只写 success.jsp 或写 /s2/success.jsp 都可, s2是部署的项目名
    // request.getContextPath()  == "/s2"
  }
}
